import os
import platform
import xbmc
import xbmc
import xbmcaddon
import xbmcgui
import xbmcvfs
from xbmcswift2 import Plugin
__addon_id__ = 'service.undermedia.sync'
__addon__ = xbmcaddon.Addon(id=__addon_id__)
__addonversion__ = __addon__.getAddonInfo('version')
__addonname__ = __addon__.getAddonInfo('name')
__addonpath__ = __addon__.getAddonInfo('path').decode('utf-8')
__icon__ = __addon__.getAddonInfo('icon')
__localize__ = __addon__.getLocalizedString

libraries = os.path.join(__addon__.getAddonInfo('Path') , 'lib')
sys.path.append (libraries)
import undermedia

plugin = Plugin(__addonname__, __addon_id__, __file__)

class Main:
    def __init__(self):
        if not sys.argv[0]:
            plugin.log.info('Setting Up this service...')
            xbmc.executebuiltin('XBMC.AlarmClock(CheckAtBoot,XBMC.RunScript(service.undermedia.sync, started),00:00:30,silent)')
            xbmc.executebuiltin('XBMC.AlarmClock(CheckWhileRunning,XBMC.RunScript(service.undermedia.sync, started),00:05:00,silent,loop)')
        elif sys.argv[0] and sys.argv[1] == 'started':
            plugin.log.info("Checking Requirements...")
            undermedia.checkRequirements()
            plugin.log.info("Get Remote Streams and Sync")
            undermedia.getMovies()
            plugin.log.info("Scanning Library")
            undermedia.scanLibrary()
            plugin.log.info("Refreshing Container")
            xbmc.executebuiltin('Container.Update(' + undermedia.LOCALVIDEOPATH + ')')
            xbmc.executebuiltin('UpdateAddonRepos') 
        else:
            pass